package br.com.main.client.system.morador;

import java.util.ArrayList;
import java.util.List;

import br.com.main.client.ComunServico;
import br.com.main.client.ComunServicoAsync;
import br.com.main.client.system.botoes.Reserva;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.sun.java.swing.plaf.windows.resources.windows;

public class ReservasPresenterM {
	public Display display;
	public ArrayList<Integer>ids = new ArrayList<Integer>();
	
	public interface Display {

		void atualizaTabela();

		void atualizaReservas(ArrayList<Reserva> res);

		void preencheLista(String[] a);

		Button getBtRegistrar();

		DateBox getDateBox();

		ListBox getLista();
		
	}
	
	public ReservasPresenterM(Display display) {
		this.display = display;
		bind();
	}

	public void bind() {
		loadReservas();
		loadLista();
		
		display.getBtRegistrar().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				
				String dataReservada = display.getDateBox().getTextBox().getValue();
				int pos = display.getLista().getSelectedIndex();
				int idArea = ids.get(pos).intValue();
				
				getServico().cadastraReserva(idArea, dataReservada, new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String arg0) {
						if (arg0.equals("OK")) {
							Window.alert("Reserva Cadastrada");
							loadReservas();
							loadLista();
							display.getDateBox().getTextBox().setText("");
							
						} else {

							Window.alert("Data da reserva não pode já estar marcada!");
						}
					}
					
					@Override
					public void onFailure(Throwable arg0) {

					}
				});
			}
		});
	}
	
	public void loadReservas() {
		getServico().carregaHistoricoReservas(new AsyncCallback<ArrayList<Reserva>>() {
			
			@Override
			public void onSuccess(ArrayList<Reserva> reservas) {
				display.atualizaReservas(reservas);
				display.atualizaTabela();
			}
			
			@Override
			public void onFailure(Throwable object) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public void loadLista() {
		getServico().carregarAreaLazer(new AsyncCallback<List<String[]>>() {

			@Override
			public void onFailure(Throwable object) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<String[]> object) {
				String[] areas = new String[object.size()];
				for (int i = 0; i < object.size(); i++) {
					areas[i] = object.get(i)[0];
					
					Integer inte = new Integer(object.get(i)[3]);
					ids.add(inte);
				}
				display.preencheLista(areas);
			}
		});
	}
	
	private ComunServicoAsync getServico() {
		return GWT.create(ComunServico.class);
	}

}
